package com.cn.shuangzi.userinfo.common;

import com.cn.shuangzi.SZBaseActivity;
import com.cn.shuangzi.bean.VipInfo;
import com.cn.shuangzi.common.SZCommonEvent;
import com.cn.shuangzi.common.SZConst;
import com.cn.shuangzi.loginplugin.bean.SZUserInfo;
import com.cn.shuangzi.util.SZDateUtil;
import com.cn.shuangzi.util.SZValidatorUtil;
import com.cn.shuangzi.util.SZXmlUtil;
import com.cn.shuangzi.view.sweet_alert.SweetAlertDialog;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;

/**
 * Created by CN.
 */

public class UserManager {
    private static UserManager INSTANCE;
    private SZUserInfo userInfo;
    private VipInfo vipInfo;
    private boolean isShownDueAlert;
    private UserManager() {
        isShownDueAlert = false;
    }

    public static UserManager getInstance() {
        if (INSTANCE == null) {
            synchronized (UserManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new UserManager();
                }
            }
        }
        return INSTANCE;
    }
    public void showVipDueAlert(SZBaseActivity activity, Class<?> buyVipClass) {
        showVipDueAlert(activity, buyVipClass, 7, "会员即将到期"
                , "您的会员即将在" + SZDateUtil.getShowYearMonthDate(new Date(getVipInfo().getMemberValidityTime())) + "到期，为了不影响您使用会员权限，请尽快续费"
                , "续费会员", "稍后再说");
    }

    public void showVipDueAlert(SZBaseActivity activity, Class<?> buyVipClass, int distanceShow) {
        showVipDueAlert(activity, buyVipClass, distanceShow, "会员即将到期"
                , "您的会员即将在" + SZDateUtil.getShowYearMonthDate(new Date(getVipInfo().getMemberValidityTime())) + "到期，为了不影响您使用会员权限，请尽快续费"
                , "续费会员", "稍后再说");
    }

    public void showVipDueAlert(final SZBaseActivity activity, final Class<?> buyVipClass, int distanceShow
            , String title, String content, String okText, String cancelText) {
        if(isShownDueAlert){
            return;
        }
        if(getVipInfo() == null){
            return;
        }
        long time = getVipInfo().getMemberValidityTime();
        int distance = SZDateUtil.getDayDistance(new Date(), new Date(time));
        if (distance > 0 && distance <= distanceShow) {
            isShownDueAlert = true;
            activity.showAlert(SweetAlertDialog.WARNING_TYPE, title, content
                    , okText, new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismissWithAnimation();
                            activity.startActivity(buyVipClass);
                        }
                    }, cancelText, new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismissWithAnimation();
                        }
                    }, true);
        }
    }
    public boolean isVip() {
        VipInfo vipInfo = getVipInfo();
        if (vipInfo != null) {
            boolean isValidTime = vipInfo.getMemberValidityTime() >= System.currentTimeMillis();
            if (!isValidTime) {
                setVipInfo(null);
            }
            return isValidTime;
        }
        return false;
    }

    public void setVipInfo(VipInfo vipInfo) {
        if (getUserInfo() != null) {
            this.vipInfo = vipInfo;
            setVipInfo(getUserInfo().getId(), vipInfo);
        }
    }

    public VipInfo getVipInfo() {
        if (getUserInfo() == null) {
            return null;
        }
        if (vipInfo == null) {
            if (getUserInfo() != null) {
                vipInfo = getVipInfo(getUserInfo().getId());
            }
        }
        return vipInfo;
    }

    private VipInfo getVipInfo(String userId) {
        try {
            if (SZValidatorUtil.isValidString(userId)) {
                String vip = new SZXmlUtil(SZConst.VIP_INFO).getString(userId);
                if (SZValidatorUtil.isValidString(vip)) {
                    return new Gson().fromJson(vip, VipInfo.class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void setVipInfo(String userId, VipInfo vipInfo) {
        if (SZValidatorUtil.isValidString(userId)) {
            if (vipInfo != null) {
                new SZXmlUtil(SZConst.VIP_INFO).put(userId, new Gson().toJson(vipInfo));
            } else {
                new SZXmlUtil(SZConst.VIP_INFO).remove(userId);
            }
        }
    }


    public SZUserInfo getUserInfo() {
        try {
            if (userInfo == null) {
                String user = new SZXmlUtil(SZConst.USER_INFO_SZ).getString(SZConst.USER_INFO_SZ);
                if (SZValidatorUtil.isValidString(user)) {
                    userInfo = new Gson().fromJson(user, SZUserInfo.class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            setUserInfo(null);
            EventBus.getDefault().post(new SZCommonEvent(SZCommonEvent.LOGOUT_EVENT));
        }
        return userInfo;
    }

    public void setUserInfo(SZUserInfo userInfo) {
        SZXmlUtil xmlUtil = new SZXmlUtil(SZConst.USER_INFO_SZ);
        if (userInfo == null) {
            xmlUtil.remove(SZConst.USER_INFO_SZ);
        } else {
            xmlUtil.put(SZConst.USER_INFO_SZ, new Gson().toJson(userInfo));
        }
        this.userInfo = userInfo;
    }

}
